package com.yuezhu.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yuezhu.R;


public class ImageLoaderManager implements ImageLoadingListener {
	private static ImageLoaderManager instance;
	private DisplayImageOptions options;
	private ImageLoader loader;
	private static Context mContext;
	private static final String FILE_URI = "file://";
	private ImageLoadingListener imageLoadingListener;

	public static ImageLoaderManager getInstance(Context context) {
		if (instance == null) {
			instance = new ImageLoaderManager(context);
			mContext = context;
		}
		return instance;
	}

	public void clearMemoryCache() {
		loader.clearMemoryCache();
	}

	private ImageLoaderManager(Context context) {
		options = new DisplayImageOptions.Builder()
				.resetViewBeforeLoading(true).cacheOnDisk(true)
				.cacheInMemory(true)
				/**
				 * Image will be reduces 2-fold until next reduce step make
				 * image smaller target size. It's fast type and it's preferable
				 * for usage in lists/grids/galleries (and other adapter-views)
				 * . Relates to
				 * android.graphics.BitmapFactory.Options.inSampleSize Note: If
				 * original image size is smaller than target size then original
				 * image won't be scaled.
				 */
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.showImageOnFail(R.drawable.ic_launcher).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).diskCacheSize(500 * 1024 * 1024)
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.diskCacheFileCount(200).threadPoolSize(3).build();
		ImageLoader.getInstance().init(config);

		loader = ImageLoader.getInstance();
	}

	/*
	 * public void loadImage(ImageView imageView, String url) { if
	 * (!url.startsWith("http")) { url = FILE_URI + url; }
	 * loader.displayImage(url, imageView, options); }
	 */

	/**
	 * 添加圆角图片
	 */
	public void setRoundBitmap(int cornerRadiusPixels) {
		options = new DisplayImageOptions.Builder()
				.resetViewBeforeLoading(true).cacheOnDisk(false)
				.cacheInMemory(false)
				/**
				 * Image will be reduces 2-fold until next reduce step make
				 * image smaller target size. It's fast type and it's preferable
				 * for usage in lists/grids/galleries (and other adapter-views)
				 * . Relates to
				 * android.graphics.BitmapFactory.Options.inSampleSize Note: If
				 * original image size is smaller than target size then original
				 * image won't be scaled.
				 */
				.displayer(new RoundedBitmapDisplayer(cornerRadiusPixels))
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565)
				/*.showImageOnFail(R.drawable.no_login_head)
				.showImageOnLoading(R.drawable.no_login_head)*/.build();

	}

	/**
	 * 添加一般图片
	 */
	public void setLoadingBitmap() {
		options = new DisplayImageOptions.Builder()
				.resetViewBeforeLoading(true).cacheOnDisk(false)
				.cacheInMemory(true)
				/**
				 * Image will be reduces 2-fold until next reduce step make
				 * image smaller target size. It's fast type and it's preferable
				 * for usage in lists/grids/galleries (and other adapter-views)
				 * . Relates to
				 * android.graphics.BitmapFactory.Options.inSampleSize Note: If
				 * original image size is smaller than target size then original
				 * image won't be scaled.
				 */
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565)
				/*.showImageOnFail(R.drawable.load_failed)*/.build();
	}

	/**
	 * 图片加载类
	 * 
	 * @param imageView
	 * @param url
	 */
	public void loadImage(final ImageView imageView, final String url) {
		loader.displayImage(url, imageView, options, this);
	}

	/**
	 * 图片加载类
	 * 
	 * @param imageView
	 * @param url
	 */
	public void loadImage(final ImageView imageView, final String url,
						  ImageLoadingListener listener) {
		loader.displayImage(url, imageView, options, listener);
	}

	public void loadImage(String url, ImageLoadingListener listener) {
		if (!url.startsWith("http")) {
			url = FILE_URI + url;
		}
		loader.loadImage(url, listener);
	}

	@Override
	public void onLoadingStarted(String s, View view) {

	}

	@Override
	public void onLoadingFailed(String s, View view, FailReason failReason) {

	}

	@Override
	public void onLoadingComplete(String s, View view, Bitmap bitmap) {

	}

	@Override
	public void onLoadingCancelled(String s, View view) {

	}
}
